//
//  KBMainViewController.swift
//  KimboWeiBoSwift
//
//  Created by kimbo on 15/12/15.
//  Copyright © 2015年 kimbo. All rights reserved.
//

import UIKit

class KBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        /* 使用自定义tabBar */
        let tabBar = KBTabBar()
        self.setValue(tabBar, forKeyPath: "tabBar")
        // 定义点击加号按钮回调闭包
        tabBar.compose = { () -> () in
            // 先判断用户是否登录,是就弹出KBComposeController,否则KBOAuthViewController
            if KBUserAccount.userLogin {
                let composeVc = KBComposeController()
                
                self.presentViewController(UINavigationController(rootViewController: composeVc), animated: true, completion: nil)
            } else {
                let composeVc = KBOAuthViewController()
                
                self.presentViewController(UINavigationController(rootViewController: composeVc), animated: true, completion: nil)
            }
        }
        
        /* 首页 */
        let homeVc = KBHomeViewController()
        addChildVc(homeVc, title: "首页", image: "tabbar_home")
        
        /* 消息 */
        let messageVc = KBMessageViewController()
        addChildVc(messageVc, title: "消息", image: "tabbar_message_center")
        
        /* 发现 */
        let discoveryVc = KBDiscoveryViewController()
        addChildVc(discoveryVc, title: "发现", image: "tabbar_discover")
        
        /* 我 */
        let profileVc = KBProfileViewController()
        addChildVc(profileVc, title: "我", image: "tabbar_profile")
    }
    
    //添加子控制器
    func addChildVc(controller: UIViewController, title: String, image: String) {
        
        controller.title = title
        
        controller.tabBarItem.image = UIImage(named: image)
        controller.tabBarItem.selectedImage = UIImage(named: image + "_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        //包一个导航控制器
        let nav = UINavigationController(rootViewController: controller)
        
        //添加到tabBarController
        addChildViewController(nav)
    }
    
   
}




